package com.dc.web.admin;

import com.dc.service.IpLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


@Controller
@RequestMapping("/admin")
public class IpLogController {

    @Autowired
    private IpLogService iplogService;

    //前往访问日志列表
    @GetMapping("/iplogs") //配置分页数据参数  每页15条，time倒序排列
    public String iplogs(@PageableDefault(size = 15,sort = {"time"},direction = Sort.Direction.DESC)
                                Pageable pageable, Model model){
        model.addAttribute("page",iplogService.listIpLog(pageable));
        return "admin/iplogs";
    }

    //前往访问日志汇总列表
    @GetMapping("/iplogs/count") //配置分页数据参数  每页15条，time倒序排列
    public String iplogsCount(Model model){
        model.addAttribute("ipCounts",iplogService.countIpByGroup());
        return "admin/iplogs-count";
    }


    @GetMapping("/iplogs/{id}/delete")
    public String delete(@PathVariable Long id,
                         RedirectAttributes attributes){
        iplogService.deleteIpLog(id);
        attributes.addFlashAttribute("iplogMsg","删除记录成功");
        return "redirect:/admin/iplogs";
    }

}
